package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.entity.SelectMediaEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ConstructAdapter;
import flc.ast.bean.MyConstructBean;
import flc.ast.bean.MyRecordBean;
import flc.ast.databinding.ActivityConstructBinding;
import flc.ast.utils.AppSpUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.RxUtil;

public class ConstructActivity extends BaseAc<ActivityConstructBinding> {

    public static String imgPath;
    public static int selPosition;
    private ConstructAdapter constructAdapter;


    @Override
    protected int onCreate() {
        return R.layout.activity_construct;
    }

    @Override
    protected void initView() {
        mDataBinding.ivConstructBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivConstructSave.setOnClickListener(this);
        mDataBinding.rvConstructList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        constructAdapter=new ConstructAdapter();
        mDataBinding.rvConstructList.setAdapter(constructAdapter);
        constructAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()){
            case R.id.ivConstructSave://保存
                saveImg();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        //选择相框
        mDataBinding.myPhoto.getModelImgView().setImageResource(constructAdapter.getItem(position).getImg());
    }

    @Override
    protected void initData() {
        List<MyConstructBean> list=new ArrayList<>();
        list.add(new MyConstructBean(R.drawable.aa01));
        list.add(new MyConstructBean(R.drawable.aa02));
        list.add(new MyConstructBean(R.drawable.aa03));
        list.add(new MyConstructBean(R.drawable.aa04));
        list.add(new MyConstructBean(R.drawable.aa05));
        list.add(new MyConstructBean(R.drawable.aa06));
        list.add(new MyConstructBean(R.drawable.aa07));
        list.add(new MyConstructBean(R.drawable.aa08));
        list.add(new MyConstructBean(R.drawable.aa09));
        list.add(new MyConstructBean(R.drawable.aa10));
        list.add(new MyConstructBean(R.drawable.aa11));
        list.add(new MyConstructBean(R.drawable.aa12));
        list.add(new MyConstructBean(R.drawable.aa13));
        list.add(new MyConstructBean(R.drawable.aa14));
        list.add(new MyConstructBean(R.drawable.aa15));
        list.add(new MyConstructBean(R.drawable.aa16));
        list.add(new MyConstructBean(R.drawable.aa17));
        list.add(new MyConstructBean(R.drawable.aa18));
        list.add(new MyConstructBean(R.drawable.aa19));
        list.add(new MyConstructBean(R.drawable.aa20));
        list.add(new MyConstructBean(R.drawable.aa21));
        constructAdapter.setList(list);
        Glide.with(mContext).load(imgPath).into(mDataBinding.myPhoto.getTfImgView());
        mDataBinding.myPhoto.getModelImgView().setImageResource(constructAdapter.getItem(selPosition).getImg());
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_pic_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<Boolean>() {

                    @Override
                    public void doBackground(ObservableEmitter<Boolean> emitter) {
                        File file=ImageUtils.save2Album(mDataBinding.myPhoto.getRetBitmap(), Bitmap.CompressFormat.PNG);
                        emitter.onNext(true);
                        saveRecord(file.getPath());
                    }

                    @Override
                    public void accept(Boolean isSave) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_album);
                    }
                });
            }
        }, 1000);
    }

    //保存记录
    private void saveRecord(String path){
        List<MyRecordBean> listSave = new ArrayList<>();
        listSave.add(new MyRecordBean(path,1,false));
        List<MyRecordBean> listSp = AppSpUtils.getRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveRecordList(listSp);
        } else {
            AppSpUtils.saveRecordList(listSave);
        }
    }

}